package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**
 * 
 * @author Lucie
 *class Delete_Item_Controler
 *Class variables:msg,flag
 *Class methods:delete_User,check_input,get_msg,set_msg
 *
 */
public class Delete_User_controler {
	
	private String msg; 
	private boolean flag;
	/**
	 * Constructor initializes the class variables
	 */
	public Delete_User_controler()
	{
		flag=true;
		msg=new String();
		msg=null;
	}
	
	/**
	 * 
	 *@return String white all of the problems that occurred in the class
	 */
	public String get_msg()
	{
		return msg;
	}
	/**
	 *  @param new_msg
	 * This method chain messages into a string ,
	 *  the purpose ultimately is to display on the screen all the failures
	 */
	public void set_msg(String new_msg)
	{
		if (msg==null)
			msg=new_msg;
		else
		msg+=new_msg;
		
	}

	/**
	 * 
	 * @param user_id
	 * this  method call the class ;serverproxy, to Contact with the server,
	 * first we check the Parameters we get (check_input) if something is wrong we return false 
	 * then we check that we are not trying to delete the user we are login with if we are we return false
	 *than we continue check by calling the method getUserdetails that return arraylist white the details of the user if the 
	 *return equal null we return false (user not exist) we check also the 3 place in the array list that indicate the role of
	 *the user,if the user is a member we can't delete him therefore we return false 
	 * if everything is OK we delete the user by calling the method "deleteUser"
	 * @return  boolean:If the calls in the method failed it returns false otherwise returns true
	 */
	public boolean delete_User(String user_id)
	{
		 ArrayList<String> str_check=new  ArrayList<String>();
		 ArrayList<String> str_user=new  ArrayList<String>();
	 
		 check_input(user_id);
		 	if (flag==false)
		 		return false;
		 	
		 	if(user_id.equals(new Integer(BusinessImplements.GetInstance().getUserLogin().getUserID()).toString()))
		 	{
		 		set_msg("\nerror\nyou are tring to delete the user you log in with "); 
		 		return false;
		 	}
		 	str_check=BusinessImplements.GetInstance().getServerProxy().getUserdetails(user_id);
		 	if(str_check.get(0)==null)
		 	{
		 		set_msg("\nuser not exist"); 
		 		return false;
		 	}
		 	if(str_check.get(3).equals("1"))
		 	{
		 		set_msg("\ncan't delete this user because he have a memmber card\n you can only chanhge his status "); 
		 		return false;
		 	}
		 
		 	if(!str_check.get(0).equals("1"))
			 	{
			 	str_user=BusinessImplements.GetInstance().getServerProxy().deleteUser(user_id);
				
				 	if (str_user.get(0).equals("false"))
				 	{
				 		set_msg("\nDelete failed\nPlease try again"); 
				 		return false;
				 	}
			 	}
			 	
		 	return flag;
 	
}
/**
 * This method checks the parameters we have received are not empty
* if they are poses flag= false;
 * @param user_id
 */
		public void check_input(String user_id)
		{
			if(user_id.equals(""))
			{
				set_msg("\nmising meember id ");
				flag=false;
			}
		
			
		}
	
	
	
}
